#!/bin/bash
if [ ! -d "entries" ]; then
        echo "You haven't initialized nci system yet... Please use \"./init\" before deleting entries."
        exit 1
fi

#check if no id entered
if [ -z "$1" ]; then
	echo "Error: Please type the ID of the entry you want to delete (Command usage: ./del <entry_id>)."
	exit 1
fi

if [ ! -f "entries/entry_$1" ]; then
	echo "Error: That entry doesn't exist."
	exit 1
else
	contents=$(cat "entries/entry_$1")
	if [ "$contents" == "[DELETED]" ]; then
		echo "Error: Already deleted."
		exit 1
	fi
fi

#confirmation for entry deletion
yn=""
while true; do
	echo "IMPORTANT: When you delete an entry, you are just removing it from HTML and RSS files, files saved in the \"entries/\" folder won't and mustn't be deleted!"
        echo "Are you sure you want to delete entry no. $1? There's NO UNDO for this action! (y/n)"
        read -r yn
        case $yn in
        [Yy]* ) truncate -s 0 "entries/entry_$1"
		echo "[DELETED]" >> "entries/entry_$1"
                echo "Entry no. $1 successfully deleted. REMEMBER: Use \"./refresh\" to make the changes take effect on your blog."
                exit 1
                ;;
        [Nn]* ) echo "Deletion cancelled. Entry no. $1 is safe for now."
                exit 1
                ;;
        * ) echo "Error: Please answer just y/n.";;
        esac
done

